package com.shop.dao;

import com.shop.model.Student;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class StudentDAO {

    //学生登录验证
    public  Student login(String studentId, String password){
        Student student = null;
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;

        try {
            connection = DBUtil.getConnection();
            String sql = "select * from student where studentId = ? and password = ?";
            if (connection != null) {
                preparedStatement = connection.prepareStatement(sql);
            }

            //studentId, password两个参数是从登录界面获取
            preparedStatement.setString(1, studentId);
            preparedStatement.setString(2, password);
            resultSet = preparedStatement.executeQuery();
            System.out.println("StudentDAO:30:"+resultSet);//用于调试


            if (resultSet.next()){
                System.out.println("StudentDAO:34:"+resultSet.getString("name")+" "+resultSet.getInt("age")+" "+resultSet.getInt("grade")+" "+resultSet.getString("studentId")+" "+resultSet.getString("password")+" "+resultSet.getString("phone"));//调试
                student = new Student(resultSet.getString("name"),
                        resultSet.getInt("age"),
                        resultSet.getInt("grade"),
                        resultSet.getString("studentId"),
                        resultSet.getString("password"),
                        resultSet.getString("phone"),
                        resultSet.getDouble("money"),
                        resultSet.getString("identity"),
                        resultSet.getString("major")
                );
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            DBUtil.close(connection, preparedStatement, resultSet);
        }

        return student;
    }

    public Student search(String studentId,String phone){
        Student student = null;
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;

        try {
            connection = DBUtil.getConnection();
            String sql = "select * from student where studentId = ? or phone = ?";
            if (connection != null) {
                preparedStatement = connection.prepareStatement(sql);
            }

            preparedStatement.setString(1, studentId);
            preparedStatement.setString(2, phone);

            resultSet = preparedStatement.executeQuery();

            if (resultSet.next()){
                student = new Student(resultSet.getString("name"),
                        resultSet.getInt("age"),
                        resultSet.getInt("grade"),
                        resultSet.getString("studentId"),
                        resultSet.getString("password"),
                        resultSet.getString("phone"),
                        resultSet.getDouble("money"),
                        resultSet.getString("major"),
                        resultSet.getString("identity")
                );
            }

        } catch (SQLException e) {
            throw new RuntimeException(e);
        }finally {
            DBUtil.close(connection, preparedStatement, resultSet);
        }
            return student;
    }

    public Boolean update(String name,int age,int grade,String studentId,String major){
        int i = 0;
        Connection connection = null;
        PreparedStatement preparedStatement = null;

        try {
            connection = DBUtil.getConnection();
            String sql = "update student set name = ?,age = ?,grade = ?,major = ? where studentId = ?";
            if (connection != null) {
                preparedStatement = connection.prepareStatement(sql);
            }
            preparedStatement.setString(1, name);
            preparedStatement.setInt(2, age);
            preparedStatement.setInt(3, grade);
            preparedStatement.setString(4, major);
            preparedStatement.setString(5, studentId);

            i = preparedStatement.executeUpdate();


        } catch (SQLException e) {
            throw new RuntimeException(e);
        }finally {
            DBUtil.close(connection, preparedStatement);
        }
        if(i!=0){
            return true;
        }else{
            return false;
        }
    }

    public Boolean updateMoney(String studentId,Double money){
        int i = 0;
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        try {
            connection = DBUtil.getConnection();
            String sql = "update student set money = ? where studentId = ?";
            if (connection != null) {
                preparedStatement = connection.prepareStatement(sql);
            }
            preparedStatement.setDouble(1, money);
            preparedStatement.setString(2, studentId);

            i = preparedStatement.executeUpdate();
        }catch (SQLException e) {
            throw new RuntimeException(e);
        }finally {
            DBUtil.close(connection, preparedStatement);
        }
        if(i!=0){
            return true;
        }else{
            return false;
        }
    }
    public Boolean updatePassword(String studentId,String password){
        int i = 0;
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        try {
            connection = DBUtil.getConnection();
            String sql = "update student set password = ? where studentId = ?";
            if (connection != null) {
                preparedStatement = connection.prepareStatement(sql);
            }
            preparedStatement.setString(1, password);
            preparedStatement.setString(2, studentId);
            i = preparedStatement.executeUpdate();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }finally {
            DBUtil.close(connection, preparedStatement);
        }
        if(i!=0){
            return true;
        }else{
            return false;
        }
    }
    public Boolean updateIdentity(String studentId){
        int i = 0;
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        try {
            connection = DBUtil.getConnection();
            String sql = "update student set identity = '学生VIP' where studentId = ?";
            if (connection != null) {
                preparedStatement = connection.prepareStatement(sql);
            }
            preparedStatement.setString(1, studentId);
            i = preparedStatement.executeUpdate();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }finally {
            DBUtil.close(connection, preparedStatement);
        }
        if(i!=0){
            return true;
        }else{
            return false;
        }
    }
}
